// Copyright (C) 1953-2022 NUDT
// Verilog module name - packet_descriptor_generate
// Version: V3.4.0.20220225
// Created:
//         by - fenglin 
////////////////////////////////////////////////////////////////////////////
// Description:
//         Frame Phase 
///////////////////////////////////////////////////////////////////////////

`timescale 1ns/1ps

module packet_descriptor_generate #(parameter inport = 6'b0000,forward_mode = 1'b0) //forward_mode:0,cutthrough; 1,stored        
    (
        i_clk,
        i_rst_n,

        iv_addr,                          
        iv_wdata,                                  
        i_wr_pdg,                         
        i_rd_pdg,                         

        o_wr_pdg,          
        ov_addr_pdg,       
        ov_rdata_pdg, 
        
        iv_data,
        i_data_wr,
		iv_ipv,
        iv_eth_type,
  
        i_pkt_bufid_wr,
        iv_pkt_bufid,
        o_pkt_bufid_ack,
        
        ov_pkt,
        o_pkt_wr,
        o_pkt_bufid_wr,
        ov_pkt_bufid,
        
        ov_descriptor        ,
        o_descriptor_wr  ,
        
        i_pkt_write_finish,
        //ov_eth_type ,
        //ov_tsmp_subtype,
        //for discarding pkt while the fifo_used_findows is over the threshold 
        iv_free_bufid_num,
        iv_hardware_stage,
        iv_rc_threshold_value           ,
        iv_hpriority_be_threshold_value  ,
        iv_lpriority_be_threshold_value           ,

        descriptor_extract_state,
        descriptor_send_state,
        data_splice_state
    );

// I/O
// clk & rst
input                   i_clk;
input                   i_rst_n;

input       [18:0]      iv_addr;                         
input       [31:0]      iv_wdata;                              
input                   i_wr_pdg;                        
input                   i_rd_pdg; 

output                  o_wr_pdg;                     
output     [18:0]       ov_addr_pdg;                  
output     [31:0]       ov_rdata_pdg;
//input
(*MARK_DEBUG="true"*) input       [8:0]       iv_data;
(*MARK_DEBUG="true"*) input                   i_data_wr;
(*MARK_DEBUG="true"*) input       [2:0]       iv_ipv;
(*MARK_DEBUG="true"*) input       [15:0]      iv_eth_type;

(*MARK_DEBUG="true"*) input                   i_pkt_bufid_wr;
(*MARK_DEBUG="true"*) input       [8:0]       iv_pkt_bufid;
(*MARK_DEBUG="true"*) output                  o_pkt_bufid_ack;

//temp ov_descriptor and ov_pkt for discarding pkt while the fifo_used_findows is over the threshold 
(*MARK_DEBUG="true"*) input       [8:0]       iv_free_bufid_num;
(*MARK_DEBUG="true"*) input       [2:0]       iv_hardware_stage;
(*MARK_DEBUG="true"*) input       [8:0]       iv_rc_threshold_value            ;
(*MARK_DEBUG="true"*) input       [8:0]       iv_hpriority_be_threshold_value  ;
(*MARK_DEBUG="true"*) input       [8:0]       iv_lpriority_be_threshold_value  ;
//output
(*MARK_DEBUG="true"*) output                  o_pkt_wr;
(*MARK_DEBUG="true"*) output      [133:0]     ov_pkt;
(*MARK_DEBUG="true"*) output                  o_pkt_bufid_wr;
(*MARK_DEBUG="true"*) output      [8:0]       ov_pkt_bufid;

(*MARK_DEBUG="true"*) output      [35:0]      ov_descriptor       ;
(*MARK_DEBUG="true"*) output                  o_descriptor_wr ;
//(*MARK_DEBUG="true"*) output      [15:0]      ov_eth_type;
//                      output      [7:0]       ov_tsmp_subtype;
input                   i_pkt_write_finish;
//state
output      [3:0]       descriptor_extract_state;
output      [1:0]       descriptor_send_state;
output      [1:0]       data_splice_state;  
//internal wire
wire        [8:0]       data_dee2das;
wire                    data_wr_dee2das;
wire                    descriptor_valid_dat2des;
wire        [11:0]      descriptor_dat2des;

wire        [31:0]       wv_traffic_police_discard_cnt_pds2cpe;
wire        [15:0]       wv_eth_type_pde2pds;
wire        [7:0]        wv_tsmp_subtype_pde2pds;
packet_descriptor_extract  #(.inport(inport)) packet_descriptor_extract_inst(
.i_clk                      (i_clk),
.i_rst_n                    (i_rst_n),

.iv_data                    (iv_data),
.i_data_wr                  (i_data_wr),
//.iv_ipv                     (iv_ipv),
//.iv_eth_type                (iv_eth_type),

.ov_data                    (data_dee2das),
.o_data_wr                  (data_wr_dee2das),
.o_descriptor_valid         (descriptor_valid_dat2des),
.ov_descriptor              (descriptor_dat2des),
.ov_eth_type                (wv_eth_type_pde2pds),
.ov_tsmp_subtype            (wv_tsmp_subtype_pde2pds),
.descriptor_extract_state   (descriptor_extract_state)
);  
//#(parameter network_port_or_hcp_port = 1'b0)//1'b0:network port.   1'b1:hcp port.    
packet_descriptor_send #(.forward_mode(forward_mode)) packet_descriptor_send_inst(  
.i_clk                      (i_clk),
.i_rst_n                    (i_rst_n),
.i_pkt_bufid_wr             (i_pkt_bufid_wr),
.iv_pkt_bufid               (iv_pkt_bufid),

.i_pkt_write_finish         (i_pkt_write_finish),
.iv_free_bufid_num          (iv_free_bufid_num),
.iv_rc_threshold_value      (iv_rc_threshold_value),
.iv_hpriority_be_threshold_value(iv_hpriority_be_threshold_value),
.iv_lpriority_be_threshold_value(iv_lpriority_be_threshold_value),
.ov_traffic_police_discard_cnt         (wv_traffic_police_discard_cnt_pds2cpe),
        
.i_descriptor_valid         (descriptor_valid_dat2des),
.iv_descriptor              (descriptor_dat2des      ),
.iv_eth_type                (wv_eth_type_pde2pds     ),
.iv_tsmp_subtype            (wv_tsmp_subtype_pde2pds ),
                     
.o_pkt_bufid_ack            (o_pkt_bufid_ack),  
.o_pkt_bufid_wr             (o_pkt_bufid_wr),
.ov_pkt_bufid               (ov_pkt_bufid),

.ov_descriptor              (ov_descriptor[11:0]       ),
.o_descriptor_wr            (o_descriptor_wr ),
.ov_eth_type                (ov_descriptor[35:20]),
.ov_tsmp_subtype            (ov_descriptor[19:12]),
.descriptor_send_state      (descriptor_send_state)
);

data_splice data_splice_inst(
.i_clk                      (i_clk),
.i_rst_n                    (i_rst_n),
.i_data_wr                  (data_wr_dee2das),
.iv_data                    (data_dee2das),
.o_pkt_wr                   (o_pkt_wr),
.ov_pkt                     (ov_pkt),
.data_splice_state          (data_splice_state)
); 

command_parse_and_encapsulate_pdg command_parse_and_encapsulate_pdg_inst(
.i_clk                         (i_clk                         ),
.i_rst_n                       (i_rst_n                       ),
.iv_pkt_discard_cnt            (wv_traffic_police_discard_cnt_pds2cpe            ),

.iv_addr                       (iv_addr                       ),                         
.iv_wdata                      (iv_wdata                      ),                        
.i_wr_dex                      (i_wr_pdg                      ),         
.i_rd_dex                      (i_rd_pdg                      ),         

.o_wr_dex                      (o_wr_pdg                      ),         
.ov_addr_dex                   (ov_addr_pdg                   ),      
.ov_rdata_dex                  (ov_rdata_pdg                  )
);    
endmodule